package com.wave.wavesystem.business.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.wavesystem.business.pojo.dto.userRelation.UserDeptDTO;
import com.wave.wavesystem.business.pojo.entity.DeptEntity;
import com.wave.wavesystem.business.pojo.entity.UserEntity;
import com.wave.wavesystem.business.pojo.vo.DeptVO;
import com.wave.wavesystem.business.service.UserDeptService;
import wave.common.pojo.entity.Query;
 import wave.common.pojo.entity.Result;
import com.wave.wavesystem.common.util.Func;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-02-02
 * @description 用户-部门关联表 访问层
 */
@RestController
@RequestMapping("/userDept")
@Tag(name = "用户-部门接口")
public class UserDeptController {

    @Resource
    private UserDeptService userDeptService;

    /**
     * 部门批量关联用户
     *
     * @param userDepts 关联部门
     */
    @Operation(summary = "部门批量关联用户")
    @PostMapping("/batchSave")
    public Result<Boolean> batchSave(@RequestBody UserDeptDTO userDepts) {
        return Result.status(userDeptService.batchSave(userDepts));
    }

    /**
     * 用户批量关联部门
     *
     * @param userDepts 关联部门
     */
    @Operation(summary = "部门关联用户")
    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody UserDeptDTO userDepts) {
        return Result.status(userDeptService.save(userDepts));
    }

    /**
     * 获取用户所在部门
     *
     * @param userId 用户id
     */
    @Operation(summary = "获取用户所在部门")
    @GetMapping("/userDepts")
    public Result<List<DeptVO>> userDepts(@RequestParam Long userId) {
        return Result.data(userDeptService.userDepts(userId));
    }

    /**
     * 分页获取部门拥有的用户
     *
     * @param deptId 部门id
     * @param query  分页数据
     */
    @Operation(summary = "分页获取部门拥有的用户")
    @GetMapping("/deptUsersPage")
    public Result<Page<UserEntity>> deptUsersPage(Long deptId, Query query) {
        if (Func.isEmpty(deptId)) return Result.data(new Page<>());
        return Result.data(userDeptService.deptUsersPage(deptId, query));
    }

    /**
     * 获取用户所在的所有部门
     *
     * @param userId 用户id
     */
    @Operation(summary = "获取用户所在的所有部门")
    @GetMapping("/userDeptsList")
    public Result<List<DeptEntity>> userDeptsList(Long userId) {
        return Result.data(userDeptService.userDeptsList(userId));
    }

    /**
     * 获取非此部门用户
     *
     * @param deptId 部门id
     */
    @Operation(summary = "获取非此部门用户")
    @GetMapping("/notInDeptUser")
    public Result<List<UserEntity>> notInDeptUser(Long deptId) {
        return Result.data(userDeptService.notInDeptUser(deptId));
    }

    /**
     * 部门移出用户
     *
     * @param userDepts 部门id 用户ids
     */
    @Operation(summary = "部门移出用户")
    @PostMapping("/kickOutDept")
    public Result<Boolean> kickOutDept(@RequestBody UserDeptDTO userDepts) {
        return Result.status(userDeptService.kickOutDept(userDepts));
    }
}